package com.yxhl.zoume.core.busticket.presenter;

import android.content.Context;

import com.yxhl.protobuf.BizOrder;
import com.yxhl.protobuf.BizOrderRequest;
import com.yxhl.protobuf.BizOrderResponse;
import com.yxhl.protobuf.ClientType;
import com.yxhl.protobuf.CommonReqParams;
import com.yxhl.protobuf.IsFerryRequest;
import com.yxhl.protobuf.IsFerryResponse;
import com.yxhl.protobuf.OneYxBusInfoRequest;
import com.yxhl.protobuf.OneYxBusInfoResponse;
import com.yxhl.zoume.common.presenter.BasePresenter;
import com.yxhl.zoume.common.presenter.IPresenter;
import com.yxhl.zoume.common.ui.view.BaseView;
import com.yxhl.zoume.core.busticket.view.ZouMeBusOrderView;
import com.yxhl.zoume.domain.interactor.ZMSubscriber;
import com.yxhl.zoume.domain.interactor.busticket.CheckIsNeedShuttleUseCase;
import com.yxhl.zoume.domain.interactor.busticket.GenerateZouMeBusOrderUseCase;
import com.yxhl.zoume.domain.interactor.busticket.GetZouMeBusDetailUseCase;

import javax.inject.Inject;

/**
 * Created by Xboy on 16/6/2.
 */

public class ZouMeBusOrderPresenter extends BasePresenter implements IPresenter {
    private ZouMeBusOrderView zouMeBusOrderView;
    private GetZouMeBusDetailUseCase getZouMeBusDetailUseCase;
    private CheckIsNeedShuttleUseCase checkIsNeedShuttleUseCase;
    private GenerateZouMeBusOrderUseCase generateZouMeBusOrderUseCase;

    @Inject
    public ZouMeBusOrderPresenter(Context context,
                                  GetZouMeBusDetailUseCase getZouMeBusDetailUseCase,
                                  CheckIsNeedShuttleUseCase checkIsNeedShuttleUseCase,
                                  GenerateZouMeBusOrderUseCase generateZouMeBusOrderUseCase) {
        super(context);
        this.getZouMeBusDetailUseCase = getZouMeBusDetailUseCase;
        this.checkIsNeedShuttleUseCase = checkIsNeedShuttleUseCase;
        this.generateZouMeBusOrderUseCase = generateZouMeBusOrderUseCase;
    }

    /**
     * 获取当前车次详情
     *
     * @param param
     */
    public void getZouMeBusDetail(OneYxBusInfoRequest param) {
        zouMeBusOrderView.waitingForZouMeBusDetailRequest(true);

        getZouMeBusDetailUseCase.execute(param, new ZMSubscriber<OneYxBusInfoResponse>() {
            @Override
            public void onCompleted() {
                super.onCompleted();
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                zouMeBusOrderView.waitingForZouMeBusDetailRequest(false);
            }

            @Override
            public void onNext(OneYxBusInfoResponse getZouMeBusDetailResponse) {
                super.onNext(getZouMeBusDetailResponse);
                zouMeBusOrderView.waitingForZouMeBusDetailRequest(false);

                if (getZouMeBusDetailResponse.getIsSucc().equals("true")) {
                    zouMeBusOrderView.renderZouMeBusDetail(getZouMeBusDetailResponse);
                } else {
                    // 错误处理
                    zouMeBusOrderView.showErrorMessage(getZouMeBusDetailResponse.getResultMsg());
                }
            }
        });
    }

    /**
     * 是否需要接驳车(依据距离计算)
     *
     * @param param
     */
    public void checkIsNeedShuttle(IsFerryRequest param) {
        zouMeBusOrderView.waitingForCheckIsNeedShuttle(true);

        checkIsNeedShuttleUseCase.execute(param, new ZMSubscriber<IsFerryResponse>() {

            @Override
            public void onCompleted() {
                super.onCompleted();
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                zouMeBusOrderView.waitingForCheckIsNeedShuttle(false);
            }

            @Override
            public void onNext(IsFerryResponse response) {
                super.onNext(response);
                zouMeBusOrderView.waitingForCheckIsNeedShuttle(false);
                zouMeBusOrderView.renderCheckIsNeedShuttle(response.getNeedCar());
            }
        });
    }

    /**
     * 确认订单
     *
     * @param requestBuilder
     */
    public void generateOrder(BizOrderRequest.Builder requestBuilder) {
        zouMeBusOrderView.waitingForGenerateOrder(true);

        CommonReqParams.Builder commReqBuilder = CommonReqParams.newBuilder();
        commReqBuilder.setClientSystem(CLIENT_SYSTEM)
                .setClientType(ClientType.USER)
                .setClientVersion(getClientVersionName())
                .setToken(getToken());
        requestBuilder.setCommonReqParams(commReqBuilder);

        // mobile
        requestBuilder.setMobile(getMobile());

        generateZouMeBusOrderUseCase.execute(requestBuilder.build(), new ZMSubscriber<BizOrderResponse>() {
            @Override
            public void onError(Throwable e) {
                super.onError(e);
                zouMeBusOrderView.waitingForGenerateOrder(false);
            }

            @Override
            public void onNext(BizOrderResponse response) {
                super.onNext(response);
                zouMeBusOrderView.waitingForGenerateOrder(false);

                if (response.getCommonRespParams().getIsSucc().equals("true")) {
                    BizOrder bizOrder = response.getOrderFieldses(0);
                    zouMeBusOrderView.renderZouMeBusOrder(bizOrder);
                } else {
                    zouMeBusOrderView.showErrorMessage(response.getCommonRespParams().getResultMsg());
                }
            }
        });
    }

    @Override
    public void onResume() {

    }

    @Override
    public void onPause() {

    }

    @Override
    public void onDestroy() {
        checkIsNeedShuttleUseCase.unsubscribe();
        generateZouMeBusOrderUseCase.unsubscribe();
        getZouMeBusDetailUseCase.unsubscribe();
    }

    @Override
    public void attachView(BaseView view) {
        this.zouMeBusOrderView = (ZouMeBusOrderView) view;
    }
}
